package com.campus.event.controller;

import com.campus.event.entity.User;
import com.campus.event.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

@RestController
@RequestMapping("/api/auth")
@RequiredArgsConstructor
public class AuthController {
    private final UserRepository userRepository;
    private final UserDetailsService userDetailsService;

    @PostMapping("/login")
    public ResponseEntity<?> login(@RequestBody User loginUser) {
        Optional<User> userOpt = userRepository.findByUsername(loginUser.getUsername());
        if (userOpt.isEmpty() || !userOpt.get().getPassword().equals(loginUser.getPassword())) {
            return ResponseEntity.status(403).body("用户名或密码错误");
        }
        // 直接返回用户信息，不再生成token
        return ResponseEntity.ok().body(userOpt.get());
    }
} 